package com.sunwayhorizo.health.business.clinic.emr.observation;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.base.BaseManager;
import com.sunwayhorizo.health.business.dto.clinic.emr.document.EmrDiagnosisDTO;
import com.sunwayhorizo.health.common.DataTypeUtil;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.rim.ControlAct;
import com.sunwayhorizo.health.hl7.rim.Observation;
import com.sunwayhorizo.health.hl7.vocabulary.ActMood;

@Name("ObservationManager")
@Stateless
public class ObservationManager extends BaseManager implements ObservationManagerLocal
{
    @Override
    public Observation newObservation(EmrDiagnosisDTO emrDiagnosisDTO) throws Hl7Exception
    {
        CE ce = null;
        if (null != emrDiagnosisDTO.getDiagnosisType() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisType().getConceptCode()))
        {
            ce = DataTypeUtil.getCE(emrDiagnosisDTO.getDiagnosisType().getCodingschemeEnName());
        }
        else
            ce = DataTypeUtil.getCE("DIAGNOS");
        Observation observation = this.getActFactory().newObservation(ActMood.EVN, ce, null);
        // 诊断名称
        if (null != emrDiagnosisDTO.getDiagnosisName() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisName().getConceptName()))
        {
            CE diagnosisName = this.getDictionaryFactory().getCeByDictionaryDTO(emrDiagnosisDTO.getDiagnosisName());
            observation.setInterpretationCode(diagnosisName);
        }
        // 诊断类型
        if (null != emrDiagnosisDTO.getDiagnosisType() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisType().getConceptCode()))
        {
            CE diagnosisType = this.getDictionaryFactory().getCeByDictionaryDTO(emrDiagnosisDTO.getDiagnosisType());
            observation.setMethodCode(diagnosisType);
        }
        // 主子诊断（标题）个数
        if (StrUtil.isNotEmpty(emrDiagnosisDTO.getMasterDiagnosticsFlag()))
        {
            observation.setTitle(DataTypeUtil.getST(emrDiagnosisDTO.getMasterDiagnosticsFlag()));
        }
        //疑似诊断标记
        if(StrUtil.isNotEmpty(emrDiagnosisDTO.getSuspectedFlag()))
        {
            observation.setStatusCode(DataTypeUtil.getCS(emrDiagnosisDTO.getSuspectedFlag()));
        }
        // 诊断创建人
        if (StrUtil.isNotEmpty(emrDiagnosisDTO.getEmpRoleId()))
        {
            observation.setCreatedBy(Long.parseLong(emrDiagnosisDTO.getEmpRoleId()));
        }
        ControlAct controlAct = newControlAct(observation);
        save(controlAct);
        return observation;
    }
    
    
    
    @Override
    public Observation newObservationNotPersist(EmrDiagnosisDTO emrDiagnosisDTO) throws Hl7Exception
    {
        CE ce = null;
        if (null != emrDiagnosisDTO.getDiagnosisType() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisType().getConceptCode()))
        {
            ce = DataTypeUtil.getCE(emrDiagnosisDTO.getDiagnosisType().getCodingschemeEnName());
        }
        else
            ce = DataTypeUtil.getCE("DIAGNOS");
        Observation observation = this.getActFactory().newObservation(ActMood.EVN, ce, null);
        // 诊断名称
        if (null != emrDiagnosisDTO.getDiagnosisName() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisName().getConceptName()))
        {
            CE diagnosisName = this.getDictionaryFactory().getCeByDictionaryDTO(emrDiagnosisDTO.getDiagnosisName());
            observation.setInterpretationCode(diagnosisName);
        }
        // 诊断类型
        if (null != emrDiagnosisDTO.getDiagnosisType() && StrUtil.isNotEmpty(emrDiagnosisDTO.getDiagnosisType().getConceptCode()))
        {
            CE diagnosisType = this.getDictionaryFactory().getCeByDictionaryDTO(emrDiagnosisDTO.getDiagnosisType());
            observation.setMethodCode(diagnosisType);
        }
        // 主子诊断（标题）个数
        if (StrUtil.isNotEmpty(emrDiagnosisDTO.getMasterDiagnosticsFlag()))
        {
            observation.setTitle(DataTypeUtil.getST(emrDiagnosisDTO.getMasterDiagnosticsFlag()));
        }
        //疑似诊断标记
        if(StrUtil.isNotEmpty(emrDiagnosisDTO.getSuspectedFlag()))
        {
            observation.setStatusCode(DataTypeUtil.getCS(emrDiagnosisDTO.getSuspectedFlag()));
        }
        // 诊断创建人
        if (StrUtil.isNotEmpty(emrDiagnosisDTO.getEmpRoleId()))
        {
            observation.setCreatedBy(Long.parseLong(emrDiagnosisDTO.getEmpRoleId()));
        }
        return observation;
    }


    @Override
    public List<Observation> newObservation(List<EmrDiagnosisDTO> emrDiagnosisList) throws Hl7Exception
    {
        if (ListUtil.isEmpty(emrDiagnosisList))
            return null;
        List<Observation> observationList = new ArrayList<Observation>();
        for (EmrDiagnosisDTO emrDiagnosisDTO : emrDiagnosisList)
        {

            observationList.add(newObservation(emrDiagnosisDTO));
        }
        return observationList;
    }
    
    public EmrDiagnosisDTO converEmrDiagnosisDTO(Observation obs) throws Hl7Exception
    {
        if(null == obs) return null;
        EmrDiagnosisDTO emrDiagnosisDTO = new EmrDiagnosisDTO();
        emrDiagnosisDTO.setMasterDiagnosticsFlag(obs.getTitle().toString());
        if(obs.getStatusCode() != null && !obs.getStatusCode().code().toString().equals("<Null:NI>"))
        {
            emrDiagnosisDTO.setSuspectedFlag(obs.getStatusCode().code().toString());
        }
        emrDiagnosisDTO.setDiagnosisName(getDictionaryFactory().getDictionaryDTOByCe(obs.getInterpretationCode()));
        emrDiagnosisDTO.setDiagnosisType(getDictionaryFactory().getDictionaryDTOByCe(obs.getMethodCode()));
      
        return emrDiagnosisDTO;
    }
    
    public List<EmrDiagnosisDTO> converEmrDiagnosisDTOList(List<Observation> obsList) throws Hl7Exception
    {
        if(ListUtil.isEmpty(obsList)) return null;
        List<EmrDiagnosisDTO> edList = new ArrayList<EmrDiagnosisDTO>();
        for(Observation obs : obsList)
        {
            EmrDiagnosisDTO emrDiagnosisDTO =  converEmrDiagnosisDTO(obs);
            edList.add(emrDiagnosisDTO);
        }
        return edList;
    }

}
